<?php
namespace app\admin\controller;
use app\common\controller\AdminBase;
/*水控管理*/
class WaterPump extends AdminBase{
	protected $beforeActionList = [
        'before_index'=>['only'=>'index'],
    ];
	function _initialize() {
		parent::_initialize();  
    }
	/*会员卡列表*/
	function index(){
		echo $this->fetch();
	}	
	
	/*卡列表数据*/
	function ajax_index(){
		$this->ajax($this->_ajax_index());
	}
	
	//设置
	function pump_set(){
		if(request()->isPost()){
			db("WaterPumb")->insert(array("card_no"=>input("card_no"),"pump_type"=>input("pump_type"),"pump_count"=>input("pump_count"),"pump_minute"=>input("pump_minute"),"opt_type"=>1));
			return $this->ajax_success();
		}else{
		  echo $this->fetch();die;
		}
	}
  	/**
	 * 还原
	 */
	function pump_reset(){
		if(request()->isPost()){
			$card_no=input("card_no","xxxx");
			$type=input("type");
			$model=db("WaterPumb")->order("id desc")->find();
			if(empty($model)){
				return $this->ajax_error("无记录，无法还原");
			}else{
				if($type==0){
					return $this->ajax_success("",$model);
				}
				db("WaterPumb")->insert(array("card_no"=>$model["card_no"],"pump_type"=>$model["pump_type"],"pump_count"=>$model["pump_count"],"pump_minute"=>$model["pump_minute"],"opt_type"=>2));
				return $this->ajax_success("");	
			}
		}else{
			echo $this->fetch();die;
		}
	}

	///////////////////////////////////////////////////////////////////////////////////
	/**
	 * 权限菜单
	 **/
	function before_index(){
		$auth_group=sp_get_auth_menu_3("10041011");
		$this->assign("menus",$auth_group);
	}
	
	/*获取卡数据*/
	private function _ajax_index(){
		$query=array(
					 'card_no'=>array("field" =>"card_no","operator" =>"="),
					 );
		$draw=input("draw",0)+1;//请求时间		 
		$where_and=join(" and ",sp_get_param_sql(request()->isPost(),$query));
		
		$total=db("WaterPumb")->where($where_and)->fetchsql(FALSE)->count(1);
		$list=db("WaterPumb")->where($where_and)->order($this->get_order())->limit($this->get_limit())->fetchsql(FALSE)->select();
		
		return array('pageData'=>$list,'total'=>$total,"draw"=>$draw);
	}
}
